﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class XBGameController : MonoBehaviour {

	public GameObject hazard;
	public Vector3 spawnValues;
	public int hazardCount;
	public float startWait;
	public float spawnWait;
	public float waveWaite;

	public Text scoreText;
	private int score;


	// Use this for initialization
	void Start () {
		score = 0;
		UpdateScore ();
		StartCoroutine (SpawnWaves ());

	}
	
	// Update is called once per frame
	void Update () {
	
	}

	IEnumerator SpawnWaves() {
		yield return new WaitForSeconds (startWait);
		while(true) {
			for (int index = 0; index < hazardCount; index++) {
				Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 
					spawnValues.y, 
					spawnValues.z);
				Quaternion spawnRotation = Quaternion.identity;
				Instantiate (hazard, spawnPosition, spawnRotation);

				yield return new WaitForSeconds (spawnWait);
			}
				
			yield return new WaitForSeconds (waveWaite);
		}
	}


	void UpdateScore() {
		scoreText.text = "score " + score;
	}

	public void AddScore(int incressScore) {
		score += incressScore;
		UpdateScore ();
	}
}
